#include "db_raw_access.h"
#include "DBCollects.h"
// ???????????
uint8_t s7_buffer[S7_BUFFER_SIZE];


/***********************DBFindStart************************/
DBPointer FindDBPointerByNumber(uint16_t dbNumber) {
  DBPointer result = {NULL, 0}; // 默认返回空指针和 0 大小
  switch(dbNumber) {
    case 1:
      result.pointer      = g_DB1_t;
      result.size         = 676;
      result.PackDBCall   = DB1_pack_to_buffer;
      result.UnPackDBCall = DB1_unpack_from_buffer;
      break;
      //    case 20:
      //      result.pointer      = g_DB20_t;
      //      result.size         = 440;
      //      result.PackDBCall   = DB20_pack_to_buffer;
      //      result.UnPackDBCall = DB20_unpack_from_buffer;
      //      break;
    case 22:
      result.pointer      = g_DB22_t;
      result.size         = 200;
      result.PackDBCall   = DB22_pack_to_buffer;
      result.UnPackDBCall = DB22_unpack_from_buffer;
      break;
      //    case 51:
      //      result.pointer      = g_DB51_t;
      //      result.size         = 960;
      //      result.PackDBCall   = DB51_pack_to_buffer;
      //      result.UnPackDBCall = DB51_unpack_from_buffer;
      //      break;
    case 52:
      result.pointer      = g_DB52_t;
      result.size         = 15600;
      result.PackDBCall   = DB52_pack_to_buffer;
      result.UnPackDBCall = DB52_unpack_from_buffer;
      break;
      //    case 60:
      //      result.pointer      = g_DB60_t;
      //      result.size         = 804;
      //      result.PackDBCall   = DB60_pack_to_buffer;
      //      result.UnPackDBCall = DB60_unpack_from_buffer;
      //      break;
      //    case 70:
      //      result.pointer      = g_DB70_t;
      //      result.size         = 79;
      //      result.PackDBCall   = DB70_pack_to_buffer;
      //      result.UnPackDBCall = DB70_unpack_from_buffer;
      //      break;
      //    case 80:
      //      result.pointer      = g_DB80_t;
      //      result.size         = 2661;
      //      result.PackDBCall   = DB80_pack_to_buffer;
      //      result.UnPackDBCall = DB80_unpack_from_buffer;
      //      break;
      //    case 81:
      //      result.pointer      = g_DB81_t;
      //      result.size         = 7800;
      //      result.PackDBCall   = DB81_pack_to_buffer;
      //      result.UnPackDBCall = DB81_unpack_from_buffer;
      //      break;
    case 91:
      result.pointer      = g_DB91_t;
      result.size         = 160;
      result.PackDBCall   = DB91_pack_to_buffer;
      result.UnPackDBCall = DB91_unpack_from_buffer;
      break;
      //    case 90:
      //      result.pointer      = g_DB90_t;
      //      result.size         = 5695;
      //      result.PackDBCall   = DB90_pack_to_buffer;
      //      result.UnPackDBCall = DB90_unpack_from_buffer;
      //      break;
    case 101:
      result.pointer      = g_DB101_t;
      result.size         = 4356;
      result.PackDBCall   = DB101_pack_to_buffer;
      result.UnPackDBCall = DB101_unpack_from_buffer;
      break;
    case 110:
      result.pointer      = g_DB110_t;
      result.size         = 346;
      result.PackDBCall   = DB110_pack_to_buffer;
      result.UnPackDBCall = DB110_unpack_from_buffer;
      break;
    case 132:
      result.pointer      = g_DB132_t;
      result.size         = 1251;
      result.PackDBCall   = DB132_pack_to_buffer;
      result.UnPackDBCall = DB132_unpack_from_buffer;
      break;
      //    case 120:
      //      result.pointer      = g_DB120_t;
      //      result.size         = 2560;
      //      result.PackDBCall   = DB120_pack_to_buffer;
      //      result.UnPackDBCall = DB120_unpack_from_buffer;
      //      break;
    default:
      break;
  }
  return result;
}
/***********************DBFindEnd************************/

///////////////////????��??

// 单字节内从右往左写 Bool
void write_bool_be(uint8_t* buffer, size_t buf_size, uint16_t byteOffset, uint8_t bitOffset, uint8_t* value) {
  if(buffer == NULL || value == NULL || byteOffset >= buf_size || bitOffset > 7)
    return;

  uint8_t bitIndex = bitOffset; // 直接使用 bitOffset（0=最低位，7=最高位）
  if(*value)
    buffer[byteOffset] |= (1 << bitIndex);
  else
    buffer[byteOffset] &= ~(1 << bitIndex);
}

// 单字节内从右往左读 Bool
void read_bool_be(const uint8_t* buffer, size_t buf_size, uint16_t byteOffset, uint8_t bitOffset, uint8_t* value) {
  if(buffer == NULL || value == NULL || byteOffset >= buf_size || bitOffset > 7) {
    if(value)
      *value = 0;
    return;
  }

  uint8_t bitIndex = bitOffset; // 直接使用 bitOffset（0=最低位，7=最高位）
  *value           = (buffer[byteOffset] >> bitIndex) & 1;
}

// 8-bit
void write_u8_be(uint8_t* buffer, size_t buf_size, uint16_t byteOffset, uint8_t bitOffset, uint8_t* value) {
  if(buffer == NULL || value == NULL || bitOffset != 0 || byteOffset >= buf_size)
    return;
  buffer[byteOffset] = *value;
}

void read_u8_be(const uint8_t* buffer, size_t buf_size, uint16_t byteOffset, uint8_t bitOffset, uint8_t* value) {
  if(buffer == NULL || value == NULL || bitOffset != 0 || byteOffset >= buf_size) {
    if(value)
      *value = 0;
    return;
  }
  *value = buffer[byteOffset];
}

// 16-bit
void write_u16_be(uint8_t* buffer, size_t buf_size, uint16_t byteOffset, uint8_t bitOffset, uint16_t* value) {
  if(buffer == NULL || value == NULL || bitOffset != 0 || byteOffset + 1 >= buf_size)
    return;
  buffer[byteOffset + 0] = (*value >> 8) & 0xFF;
  buffer[byteOffset + 1] = (*value >> 0) & 0xFF;
}

void read_u16_be(const uint8_t* buffer, size_t buf_size, uint16_t byteOffset, uint8_t bitOffset, uint16_t* value) {
  if(buffer == NULL || value == NULL || bitOffset != 0 || byteOffset + 1 >= buf_size) {
    if(value)
      *value = 0;
    return;
  }
  *value = ((uint16_t) buffer[byteOffset] << 8) | ((uint16_t) buffer[byteOffset + 1]);
}

// 32-bit
void write_u32_be(uint8_t* buffer, size_t buf_size, uint16_t byteOffset, uint8_t bitOffset, uint32_t* value) {
  if(buffer == NULL || value == NULL || bitOffset != 0 || byteOffset + 3 >= buf_size)
    return;
  buffer[byteOffset + 0] = (*value >> 24) & 0xFF;
  buffer[byteOffset + 1] = (*value >> 16) & 0xFF;
  buffer[byteOffset + 2] = (*value >> 8) & 0xFF;
  buffer[byteOffset + 3] = (*value >> 0) & 0xFF;
}

void read_u32_be(const uint8_t* buffer, size_t buf_size, uint16_t byteOffset, uint8_t bitOffset, uint32_t* value) {
  if(buffer == NULL || value == NULL || bitOffset != 0 || byteOffset + 3 >= buf_size) {
    if(value)
      *value = 0;
    return;
  }
  *value = ((uint32_t) buffer[byteOffset + 0] << 24) | ((uint32_t) buffer[byteOffset + 1] << 16) |
           ((uint32_t) buffer[byteOffset + 2] << 8) | ((uint32_t) buffer[byteOffset + 3]);
}

// 64-bit
void write_u64_be(uint8_t* buffer, size_t buf_size, uint16_t byteOffset, uint8_t bitOffset, uint64_t* value) {
  if(buffer == NULL || value == NULL || bitOffset != 0 || byteOffset + 7 >= buf_size)
    return;
  buffer[byteOffset + 0] = (*value >> 56) & 0xFF;
  buffer[byteOffset + 1] = (*value >> 48) & 0xFF;
  buffer[byteOffset + 2] = (*value >> 40) & 0xFF;
  buffer[byteOffset + 3] = (*value >> 32) & 0xFF;
  buffer[byteOffset + 4] = (*value >> 24) & 0xFF;
  buffer[byteOffset + 5] = (*value >> 16) & 0xFF;
  buffer[byteOffset + 6] = (*value >> 8) & 0xFF;
  buffer[byteOffset + 7] = (*value >> 0) & 0xFF;
}

void read_u64_be(const uint8_t* buffer, size_t buf_size, uint16_t byteOffset, uint8_t bitOffset, uint64_t* value) {
  if(buffer == NULL || value == NULL || bitOffset != 0 || byteOffset + 7 >= buf_size) {
    if(value)
      *value = 0;
    return;
  }
  *value = ((uint64_t) buffer[byteOffset + 0] << 56) | ((uint64_t) buffer[byteOffset + 1] << 48) |
           ((uint64_t) buffer[byteOffset + 2] << 40) | ((uint64_t) buffer[byteOffset + 3] << 32) |
           ((uint64_t) buffer[byteOffset + 4] << 24) | ((uint64_t) buffer[byteOffset + 5] << 16) |
           ((uint64_t) buffer[byteOffset + 6] << 8) | ((uint64_t) buffer[byteOffset + 7]);
}

// char[]
void write_char_array_be(
    uint8_t* buffer, size_t buf_size, uint16_t byteOffset, uint8_t bitOffset, char* str, size_t str_len) {
  if(buffer == NULL || str == NULL || bitOffset != 0 || byteOffset + str_len > buf_size)
    return;
  for(size_t i = 0; i < str_len; ++i) {
    buffer[byteOffset + i] = (uint8_t) str[i];
  }
}

void read_char_array_be(
    const uint8_t* buffer, size_t buf_size, uint16_t byteOffset, uint8_t bitOffset, char* str, size_t str_len) {
  if(buffer == NULL || str == NULL || bitOffset != 0 || byteOffset + str_len > buf_size)
    return;
  for(size_t i = 0; i < str_len; ++i) {
    str[i] = (char) buffer[byteOffset + i];
  }
}
